/*
 * Copyright (C) 2013 Andreas Stuetz <andreas.stuetz@gmail.com>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.hoangkien.fragment;

import java.util.ArrayList;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;

import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshBase.Mode;
import com.handmark.pulltorefresh.library.PullToRefreshBase.OnRefreshListener;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.hoangkien.apdater.AdapterAllSoundCloud;
import com.hoangkien.config.SoundCloudConfig;
import com.hoangkien.entity.SoundCloudEntity;
import com.hoangkien.service.SoundCloudService;
import com.hoangkien.soundcloud.R;
import com.hoangkien.soundcloud.activity.PlayMusicActivity;

public class AllSoundCloudFragment extends Fragment {

	private static final String ARG_POSITION = "position";
	@SuppressWarnings("unused")
	private int position;
	
	private PullToRefreshListView listSoundCloud;
	private SoundCloudService soundCloudService;
	private AdapterAllSoundCloud adapter;
	private ArrayList<SoundCloudEntity> mArrayList = new ArrayList<SoundCloudEntity>();

	public static AllSoundCloudFragment newInstance(int position) {
		AllSoundCloudFragment f = new AllSoundCloudFragment();
		Bundle b = new Bundle();
		b.putInt(ARG_POSITION, position);
		f.setArguments(b);
		return f;
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		position = getArguments().getInt(ARG_POSITION);
		soundCloudService = new SoundCloudService(getActivity());
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		//dang ky broadcast
		container.getContext().registerReceiver(receive, intentFilter);
		container.getContext().registerReceiver(receive, intentFilter1);
		LayoutInflater inflater2 = LayoutInflater.from(getActivity());
		View view = inflater2.inflate(R.layout.layout_pull_listview, null);
		listSoundCloud = (PullToRefreshListView) view.findViewById(R.id.listView_soundcloud);
		listSoundCloud.setMode(Mode.BOTH);
		mArrayList = soundCloudService.getAllSoundCloud();
		adapter = new AdapterAllSoundCloud(getActivity(), mArrayList);
		listSoundCloud.setAdapter(adapter);
		listSoundCloud.setOnRefreshListener(new OnRefreshListener<ListView>() {
			@Override
			public void onRefresh(PullToRefreshBase<ListView> refreshView) {
				new Handler().post(new Runnable() {
					@Override
					public void run() {
						listSoundCloud.onRefreshComplete();
					}
				});
			}
		});
		
		return view;
	}
	
	IntentFilter intentFilter = new IntentFilter(SoundCloudConfig.BROADCAST_LOAD_DATA);
	IntentFilter intentFilter1 = new IntentFilter(SoundCloudConfig.BROADCAST_REFRESH_DATA);
	BroadcastReceiver receive = new BroadcastReceiver(){
		@Override
		public void onReceive(Context context, Intent intent) {
			if (intent.getAction().equals(SoundCloudConfig.BROADCAST_LOAD_DATA)
					|| intent.getAction().equals(SoundCloudConfig.BROADCAST_REFRESH_DATA)) {
				mArrayList = new ArrayList<SoundCloudEntity>();
				adapter = new AdapterAllSoundCloud(getActivity(), mArrayList);
				listSoundCloud.setAdapter(adapter);
				try {
					Thread.sleep(1000);
				} catch (InterruptedException e) {}
				mArrayList = soundCloudService.getAllSoundCloud();
				adapter = new AdapterAllSoundCloud(getActivity(), mArrayList);
				listSoundCloud.setAdapter(adapter);
			}
		}
	};

}